/*
 * Copyright 2020-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sagacity.framework.web.controller;

import com.sagacity.framework.api.model.request.GenericQueryBO;
import com.sagacity.framework.api.model.response.PageResult;
import com.sagacity.framework.api.model.response.ResponseEntity;
import com.sagacity.framework.api.service.BaseRemoteService;
import com.sagacity.framework.service.Service;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

/**
 * 控制器
 * @author xingyun
 * @date 2020-07-05 10:47
 */
public class BaseController<S extends Service<T>,T> implements BaseRemoteService<T> {

    @Autowired
    protected S service;

    @ApiOperation("新增接口")
    @Override
    public ResponseEntity<Integer> insert(@Valid @RequestBody T entity) {
        return ResponseEntity.ok(service.insert(entity));
    }

    @ApiOperation("根据ID物理删除接口")
    @Override
    public ResponseEntity<Integer> deleteById(@RequestBody T entity) {
        return ResponseEntity.ok(service.deleteById(entity));
    }

    @ApiOperation("根据ID逻辑删除接口")
    @Override
    public ResponseEntity<Integer> disableById(@RequestBody T entity) {
        return ResponseEntity.ok(service.disableById(entity));
    }

    @ApiOperation("根据ID更新接口")
    @Override
    public ResponseEntity<Integer> updateById(@RequestBody T entity) {
        return ResponseEntity.ok(service.updateById(entity));
    }

    @ApiOperation("根据主键ID查询接口")
    @Override
    public ResponseEntity<T> getById(@Valid @NotNull(message = "主键ID不能为空") Serializable id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("根据ID列表查询接口")
    @Override
    public ResponseEntity<List<T>> getByIds(@RequestParam("idList") List<Serializable> idList) {
        return ResponseEntity.ok(service.selectBatchIds(idList));
    }

    @ApiOperation("根据条件查询单条数据接口")
    @Override
    public ResponseEntity<T> get(T entity) {
        return ResponseEntity.ok(service.selectOne(entity));
    }

    @ApiOperation("列表查询接口")
    @Override
    public ResponseEntity<List<T>> list(T entity) {
        return ResponseEntity.ok(service.selectList(entity));
    }

    @ApiOperation("通用分页查询接口")
    @Override
    public ResponseEntity<PageResult<T>> search(@RequestBody GenericQueryBO<T> queryBO) {
        return ResponseEntity.ok(service.search(queryBO));
    }

    @ApiOperation("导出数据接口")
    @Override
    public void export(@RequestBody GenericQueryBO<T> queryBO) {
        service.export(queryBO);
    }


}
